package com.example.doma.contraller;

import com.example.doma.pojo.dorm;
import com.example.doma.pojo.student;
import com.example.doma.service.dormService;
import com.example.doma.service.studentService;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class studentcontraller {

    @Autowired
    studentService studentService;
    @Autowired
    dormService dormService;
    @GetMapping("/student")
    public String Studentmt(Model model, HttpServletRequest request) {
        Integer Page = 0;
        if (request.getParameter("page") == null) {
            Page = 1;
        } else {
            Page = Integer.valueOf(request.getParameter("page"));
        }
        PageInfo<student> students = studentService.getStudent(Page, 30);

        model.addAttribute("students", students.getList());
        model.addAttribute("pages", students.getNavigateLastPage());
        model.addAttribute("pa", Page);
        return "admin-student";
    }

    @RequestMapping("/student/add")
    public String studentadd() {
        return "admin-student-add";
    }

    @PostMapping("/student/add")
    public String studentAdd(Model model,@RequestParam("id") int id, @RequestParam("name") String name, @RequestParam("sex") char sex, @RequestParam("faculty_name") String faculty_name, @RequestParam("spec_name") String spec_name, @RequestParam("class_name") String class_name, @RequestParam("apart_num") String apart_num, @RequestParam("dormi_num") int dormi_num, @RequestParam("bed_num") int bed_num, @RequestParam("phone1") String phone1) {

        if (studentService.isstudent(id))
        {
            model.addAttribute("msg","学生已存在");
            return "admin-student-add";
        }
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("name", name);
        map.put("sex", studentService.issex(sex));
        map.put("faculty_name", faculty_name);
        map.put("spec_name", spec_name);
        map.put("class_name", class_name);
        map.put("apart_num", apart_num);
        map.put("dormi_num", dormi_num);
        map.put("bed_num", bed_num);
        map.put("phone1", phone1);
        System.out.println(sex);
        studentService.addStudent(map);
        model.addAttribute("msg","添加成功");
        return "admin-student-add";
    }

    @GetMapping("/student/modify")
    public String modifyStudent(Model model, HttpServletRequest request) {
        System.out.println(request.getParameter("id"));
        student student = studentService.getstudentByid(Integer.valueOf(request.getParameter("id")));
        model.addAttribute("student", student);
        return "admin-student-modify";
    }

    @PostMapping("/student/modify")
    public String modifystudent( Model model, @RequestParam("id") int id, @RequestParam("name") String name, @RequestParam("sex") char sex, @RequestParam("faculty_name") String faculty_name, @RequestParam("spec_name") String spec_name, @RequestParam("class_name") String class_name, @RequestParam("apart_num") String apart_num, @RequestParam("dormi_num") int dormi_num, @RequestParam("bed_num") int bed_num, @RequestParam("phone1") String phone1) {

        student student = new student();
        student.setId(id);
        student.setName(name);
        student.setSex(studentService.issex(sex));
        student.setFaculty_name(faculty_name);
        student.setApart_num(apart_num);
        student.setPhone1(phone1);
        student.setSpec_name(spec_name);
        student.setClass_name(class_name);
        student.setDormi_num(dormi_num);
        student.setBed_num(bed_num);
        studentService.update(student);
        model.addAttribute("msg","修改成功");
        model.addAttribute("student",studentService.getstudentByid(id));
        return "admin-student-modify.html";
    }

    @PostMapping("/student/souch")
    public String souchStudent(Model model, @RequestParam("id") int id)
    {
        System.out.println(id);
        student student = studentService.getstudentByid(id);
        List<student> st=new ArrayList<>();
        st.add(student);
        model.addAttribute("students",st);
        model.addAttribute("pages",1);
        return "admin-student";
    }
    //删除操作
    @GetMapping("/student/dele")
    public  String deletestudent(HttpServletRequest request)
    {
        System.out.println(request.getParameter("id"));
        studentService.deletestudent(Integer.valueOf(request.getParameter("id")));
        return "redirect:/student";
    }
    //调寝功能
    @RequestMapping("/student/setdoma")
    public String setdoma(Model model, @Param("id") int id)
    {
        System.out.println("学生id为"+id+"调寝");
        List<Map<String, Object>> maps = studentService.getdoormsById(id);
        model.addAttribute("DORMS",maps);
        model.addAttribute("ID",id);
        return "admin-student-setdoma";
    }
    //提交调寝表单
    @GetMapping("/student/getdoma/{id}")
    public  String getforme(@PathVariable("id") Integer id,HttpServletRequest request,Model model)
    {


        //判断下拉框是否选择，优先下拉框选择
      if (request.getParameter("Dormi_num").length()==0)
      {
         if (request.getParameter("apart_num").length()==0)
         {
             List<Map<String, Object>> maps = studentService.getdoormsById(id);
             model.addAttribute("ID",id);
             model.addAttribute("DORMS",maps);
             return "admin-student-setdoma";
         }
         else {
             studentService.setDorm(id,request.getParameter("apart_num"),Integer.valueOf(request.getParameter("dormi_num")),Integer.valueOf(request.getParameter("bed_num")));
             model.addAttribute("rgs","添加成功");
             model.addAttribute("ID",id);
             model.addAttribute("DORMS",studentService.getdoormsById(id));
             return "admin-student-setdoma";
         }
      }
        System.out.println("调寝表单参数：："+id);
        System.out.println("修改到的寝室；；"+request.getParameter("Dormi_num"));
       //将获取到的参数进行解析
        Map<String, Object> dormi_num = studentService.getApart_numAndDormi_numAndbed_num(request.getParameter("Dormi_num"));


        System.out.println(dormi_num.get("apart_num"));
        System.out.println(dormi_num.get("dormi_num"));
        System.out.println(dormi_num.get("bed_num"));
        //传参提交学生调寝功能
        studentService.setDorm(id,(String)dormi_num.get("apart_num"),(int) dormi_num.get("dormi_num"),(int) dormi_num.get("bed_num"));
        //提交成功返回参数
        List<Map<String, Object>> maps = studentService.getdoormsById(id);
        model.addAttribute("ID",id);
        model.addAttribute("DORMS",maps);
        model.addAttribute("rgs","添加成功");
        return "admin-student-setdoma";
    }
    //检索功能
    @RequestMapping("/student/getdoma/souch")
public String souchisdoma(HttpServletRequest request,Model model)

    {
        System.out.println("检索功能收到参数"+request.getParameter("apart")+request.getParameter("dormi")+request.getParameter("bed"));
        List<Map<String, Object>> maps = studentService.getdoormsById(Integer.valueOf(request.getParameter("id")));
        //判断该床号是否有人
        boolean b = dormService.ishavabed_num(request.getParameter("apart"), Integer.valueOf(request.getParameter("dormi")), Integer.valueOf(request.getParameter("bed")));
        if (b)
        {
            model.addAttribute("rgs","该床位已有人");
        }
        else
        {
            model.addAttribute("rgs","该床位可申请");
        }
        model.addAttribute("DORMS",maps);
        model.addAttribute("ID",request.getParameter("id"));
        model.addAttribute("apart",request.getParameter("apart"));
        model.addAttribute("dormi",request.getParameter("dormi"));
        model.addAttribute("bed",request.getParameter("bed"));
        return "admin-student-setdoma";
    }

    //批量添加功能
    @RequestMapping("/student/adds")
    public  String adds()
    {
        return "admin-student_excel";
    }

    //批量添加，提交功能
    @PostMapping("/student/uploadExcel")
    @ResponseBody
    public  String uploadExcel(@RequestParam("file") MultipartFile file)throws IOException {
        System.out.println("---------");
        System.out.println("收到批量添加学生信息请求");
        HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
        studentService.AddStudents(workbook);

        return "添加成功";
    }

    //将学生信息打包成excel文件
    @GetMapping("/student/DowendDomas")
    public  void  DowendDomas(HttpServletResponse response) throws IOException {
        System.out.println("--------导出学生信息-----");
        List<student> students = studentService.getstudents();
        String[] head={"学号","姓名","性别","院级","专业","年级","班级","公寓","寝室","床号","电话"};
        HSSFWorkbook sheets = new HSSFWorkbook();
        HSSFSheet sheet1 = sheets.createSheet("Sheet1");
        HSSFRow row_1 = sheet1.createRow(0);
        for (int i=0;i<head.length;i++)
        {
            row_1.createCell(i).setCellValue(head[i]);
        }
        for (int i=0;i<students.size();i++)
        {
            HSSFRow row = sheet1.createRow(i + 1);
            student student = students.get(i);

            row.createCell(0).setCellValue(student.getId());
            row.createCell(1).setCellValue(student.getName());
            if (student.getSex()==1)
                row.createCell(2).setCellValue("男");
            else
                row.createCell(2).setCellValue("女");

            row.createCell(3).setCellValue(student.getFaculty_name());
            row.createCell(4).setCellValue(student.getSpec_name());
            row.createCell(5).setCellValue(student.getGrade());
            row.createCell(6).setCellValue(student.getClass_name());
            row.createCell(7).setCellValue(student.getApart_num());
            row.createCell(8).setCellValue(student.getDormi_num());
            row.createCell(9).setCellValue(student.getBed_num());
            row.createCell(10).setCellValue(student.getPhone1());
        }
        sheets.setActiveSheet(0);
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition","attachment;fileName=students.xls");
        ServletOutputStream outputStream = response.getOutputStream();
        sheets.write(outputStream);
        System.out.println("------导出成功--------");
        return ;
    }

}
